1
2 // MultiFace Recognition System.
3 // Copyright (C) 2011 Philip Cesar B. Garay
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19
20 using System;
21 using System.Collections.Generic;
22 using System.Drawing;
23 using System.Windows.Forms;
24 using Emgu.CV;
25 using Emgu.CV.Structure;
26 using Emgu.CV.CvEnum;
27 using System.IO;
28
29 namespace FaceRecognition
30 {
31 public partial class FrmPrincipal : Form
32 {
33 Image<Bgr, Byte> currentFrame;
34 Capture grabber;
35 HaarCascade face;
36 HaarCascade eye;
37 MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
38 Image<Gray, byte> result, TrainedFace = null;
39 Image<Gray, byte> gray = null;
40 List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
41 List<string> labels= new List<string>();
42 List<string> NamePersons = new List<string>();
43 int ContTrain, NumLabels, t;
44 string name, names = null;
45
46
47 public FrmPrincipal()
48 {
49 InitializeComponent();
50
51 face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
52 eye = new HaarCascade("haarcascade_eye.xml");
53 try
54 {
55 string Labelsinfo = File.ReadAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt");
56 string[] Labels = Labelsinfo.Split('%');
57 NumLabels = Convert.ToInt16(Labels[0]);
58 ContTrain = NumLabels;
59 string LoadFaces;
60
61 for (int tf = 1; tf < NumLabels+1; tf++)
62 {
63 LoadFaces = "face" + tf + ".bmp";
64 trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/TrainedFaces/" + LoadFaces));
65 labels.Add(Labels[tf]);
66 }
67
68 }
69 catch(Exception e)
70 {
71 MessageBox.Show("Nothing in binary database, please add at least a face", "Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
72 }
73
74 }
75
76
77 private void button1_Click(object sender, EventArgs e)
78 {
79 frmEnrollment frm = new frmEnrollment();
80 frm.ShowDialog();
81 }
82 void FrameGrabber(object sender, EventArgs e)
83 {
84 label3.Text = "0";
85
86 NamePersons.Add("");
87
88
89 currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
90
91 gray = currentFrame.Convert<Gray, Byte>();
92
93 MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
94 face,
95 1.2,
96 10,
97 Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
98 new Size(20, 20));
99
100 foreach (MCvAvgComp f in facesDetected[0])
101 {
102 t = t + 1;
103 result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
104 currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
105
106
107 if (trainingImages.ToArray().Length != 0)
108 {
109 MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
110
111 EigenObjectRecognizer recognizer = new EigenObjectRecognizer(
112 trainingImages.ToArray(),
113 labels.ToArray(),
114 5000,
115 ref termCrit);
116
117 name = recognizer.Recognize(result);
118
119 currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));
120
121 }
122
123 NamePersons[t-1] = name;
124 NamePersons.Add("");
125
126
127 label3.Text = facesDetected[0].Length.ToString();
128
129 /*
130 gray.ROI = f.rect;
131 MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
132 eye,
133 1.1,
134 10,
135 Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
136 new Size(20, 20));
137 gray.ROI = Rectangle.Empty;
138
139 foreach (MCvAvgComp ey in eyesDetected[0])
140 {
141 Rectangle eyeRect = ey.rect;
142 eyeRect.Offset(f.rect.X, f.rect.Y);
143 currentFrame.Draw(eyeRect, new Bgr(Color.Blue), 2);
144 }
145 */
146
147 }
148 t = 0;
149
150 for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
151 {
152 names = names + NamePersons[nnn] + ", ";
153 }
154
155 imageBoxFrameGrabber.Image = currentFrame;
156 label4.Text = names;
157 names = "";
158
159 NamePersons.Clear();
160
161 }
162
163 private void FrmPrincipal_Load(object sender, EventArgs e)
164 {
165 grabber = new Capture();
166 grabber.QueryFrame();
167 Application.Idle += new EventHandler(FrameGrabber);
168 }
169
170
171 void AboutToolStripMenuItemClick(object sender, EventArgs e)
172 {
173 frmAbout f = new frmAbout();
174 f.ShowDialog();
175 }
176
177 void ExitToolStripMenuItemClick(object sender, EventArgs e)
178 {
179 Application.Exit();
180 }
181
182 private void groupBox2_Enter(object sender, EventArgs e)
183 {
184
185 }
186 }
187 }